package com.mgg.user.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.mgg.common.dao.DaoSupport;
import com.mgg.user.service.store.StoreService;
import com.mgg.user.vo.UserStoreVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("storeService")
public class StoreServiceImpl implements StoreService{
	
	@Autowired
	private DaoSupport dao;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String,Object>> listStore(Double lng, Double lat, String storeName, Pageable page) {
		PageHelper.startPage(page.getPageNumber(), page.getPageSize());
		Map<String,Object> queryMap = new HashMap<>();
		queryMap.put("lng", lng);
		queryMap.put("lat", lat);
		if (StringUtils.isNotEmpty(storeName)) {			
			queryMap.put("store_name", storeName);
		}
		List<Map<String,Object>> storeList = dao.findForList("StoreMapper.listStore", queryMap);
		return storeList;
	}

    @Override
    public List<Map<String, Object>> listStoreByName(String name, Pageable page) {
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        List<Map<String,Object>> storeList = dao.findForList("StoreMapper.listStoreByName", name);
        return storeList;
    }

	@Override
	public Integer countStoreNum() {
	    Integer count = dao.findForObject("StoreMapper.countStoreNum", null);
		return count;
	}

    @Override
    public Integer insertUserStore(String userId, String storeCode, Boolean isSelected) {
	    Map<String,Object> map = new HashMap<>();
	    map.put("userId", userId);
	    map.put("storeCode",storeCode);
        map.put("isSelected", isSelected);
        Integer count = dao.save("StoreMapper.insertUserStore",map);
        return count;
    }

    @Override
    public Integer deleteUserStore(String userId, String storeCode) {
        Map<String,String> map = new HashMap<>();
        map.put("userId", userId);
        map.put("storeCode",storeCode);
        Integer count = dao.delete("StoreMapper.deleteUserStore",map);
        return count;
    }

    @Override
    public List<UserStoreVO> listUserStore(String userId) {
        List<UserStoreVO> userStoreVOList = dao.findForList("StoreMapper.listUserStore",userId);
        return userStoreVOList;
    }

    @Override
    public UserStoreVO getSelectedStore(String userId) {
	    UserStoreVO userStoreVO = dao.findForObject("StoreMapper.getSelectedStore",userId);
        return userStoreVO;
    }

    @Override
    public Integer updateUserStore(String userId, String storeCode, Boolean isSelected) {
        Map<String,Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("storeCode",storeCode);
        map.put("isSelected", isSelected);
        Integer count = dao.save("StoreMapper.updateUserStore",map);
        return count;
    }
}
